#!/bin/bash
###
 # @Author: dvlproad
 # @Date: 2023-03-02 14:58:30
 # @LastEditors: dvlproad
 # @LastEditTime: 2023-03-03 11:27:52
 # @Description: 
### 
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
Example_HomeDir_Absolute=${CurrentDIR_Script_Absolute} # 使用此方法可以避免路径上有..
CategoryFun_HomeDir_Absolute=${Example_HomeDir_Absolute%/*} # 使用 %/* 方法可以避免路径上有..
bulidScriptCommon_dir_Absolute=${CategoryFun_HomeDir_Absolute}/bulidScriptCommon
# cd ${CurrentDIR_Script_Absolute} # 避免内部获取workspace时候取的不是这个，而出错的问题

qscript_path_get_filepath="${bulidScriptCommon_dir_Absolute}/qscript_path_get.sh"
qbase_update_json_file_singleString_script_path="$(sh ${qscript_path_get_filepath} qbase update_json_file_singleString)"

# 2、获取 WORKSPACE 上级目录，以对 WORKSPACE 添加写权限，从来才能允许内部有在 WORKSPACE 中 git clone 的写入操作
#WORKSPACE_DIR_PATH=$CurrentDIR_Script_Absolute/..
WORKSPACE_DIR_PATH="${CurrentDIR_Script_Absolute%/*}" # 使用此方法可以避免路径上有..


WORKSPACE_PARENT_DIR_PATH_Absolute=${WORKSPACE_DIR_PATH%/*}
WORKSPACE_PARENT_DIR_NAME="${WORKSPACE_PARENT_DIR_PATH_Absolute##*/}"
# echo "WORKSPACE_PARENT_DIR_NAME=${WORKSPACE_PARENT_DIR_NAME}"

cd ${WORKSPACE_PARENT_DIR_PATH_Absolute}
sudo chmod o+w ${WORKSPACE_PARENT_DIR_NAME}
# cd ${WORKSPACE_DIR_PATH}

# 3、执行脚本

Environment_FILE_PATH="${CurrentDIR_Script_Absolute}/app_info.json"

RESULT_SALE_TO_JSON_FILE_PATH="${CurrentDIR_Script_Absolute}/app_branch_info.json" 
if [ ! -f "${RESULT_SALE_TO_JSON_FILE_PATH}" ]; then
    exit 1
else
    sh ${qbase_update_json_file_singleString_script_path} -jsonF "${Environment_FILE_PATH}" -k "branchsResultJsonFile" -v "${RESULT_SALE_TO_JSON_FILE_PATH}"
    if [ $? != 0 ]; then
        echo "❌Error:为了测试本脚本，您的 branchsResultJsonFile RESULT_SALE_TO_JSON_FILE_PATH=${RESULT_SALE_TO_JSON_FILE_PATH}存储失败，请检查！"
        exit 1
    fi
fi


History_Versions_FILE_PATH="${CurrentDIR_Script_Absolute}/app_history_version.json"
if [ ! -f "${History_Versions_FILE_PATH}" ];then
    echo "❌Error:您的History_Versions_FILE_PATH=${History_Versions_FILE_PATH}文件不存在，请检查！"
    exit 1
fi
sh ${qbase_update_json_file_singleString_script_path} -jsonF "${Environment_FILE_PATH}" -k "historyVersionJsonFile" -v "${History_Versions_FILE_PATH}"
if [ $? != 0 ]; then
    echo "❌Error:为了测试本脚本，您的 historyVersionJsonFile 值History_Versions_FILE_PATH=${History_Versions_FILE_PATH}存储失败，请检查！"
    exit 1
fi


sh ${CurrentDIR_Script_Absolute}/all_packing_info.sh -resultSaveToJsonF "${RESULT_SALE_TO_JSON_FILE_PATH}"